package mumu.android.localstorage;

import mumu.android.R;
import mumu.android.lib.localstorage.NewsDAO;
import mumu.android.lib.localstorage.NewsEn;
import android.app.Dialog;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

public class sqlitedf extends DialogFragment {

	static sqlitedf instance;

	public static sqlitedf getInstance() {
		if (instance == null) {
			instance = new sqlitedf();
		}
		return instance;
	}

	Dialog dialog;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		if (dialog == null) {
			dialog = new Dialog(getActivity(), R.style.dialog);
			LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
			View view = layoutInflater.inflate(R.layout.dialog_listview, null);
			view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			dialog.setContentView(R.layout.dialog_listview);
			dialog.setCanceledOnTouchOutside(true);
			
			
			
			useDb();
		}
		return dialog;
	}
	
	public void addNews(){
		NewsEn newsEn = new NewsEn();
		newsEn.newsid = 1;
		newsEn.defaultlogo = "aa";
		newsEn.newstitle = "叙利亚";
		NewsDAO.Instance(getActivity()).add(newsEn);
	}

	public static final String CREATE_SQL = "create table news  ( id int primary key ,  newsid int,  newstitle varchar(200) ,  defaultlogo varchar(400)  ) ";

	/**
	 * 普通调用方式
	 */
	public void useDb() {
		//这句用于创建数据库文件，存在则打开，不存在新建
		SQLiteDatabase db = getActivity().openOrCreateDatabase("mumu.db", Context.MODE_PRIVATE, null);
		// 建表
		 db.execSQL("DROP TABLE IF EXISTS news");  
		db.execSQL(CREATE_SQL);
		//插入数据
		NewsEn newsEn = new NewsEn();
		newsEn.newsid = 1;
		newsEn.defaultlogo = "aa";
		newsEn.newstitle = "叙利亚";
		db.execSQL("INSERT INTO news VALUES (1,?, ?, ?)", new Object[] { newsEn.newsid, newsEn.defaultlogo, newsEn.newstitle });
		//查询数据
		//db.rawQuery(sql, selectionArgs);
		//除了使用execSQL方法执行sql语句外，还提供了以下三个更为方便简介的方法。
		//db.delete(table, whereClause, whereArgs)
		//db.update(table, values, whereClause, whereArgs)
		//db.insert(table, nullColumnHack, values)
		
	}
}
